package com.crazymo.jni;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.crazymo.jni.databinding.ActivityMainBinding;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author cmo
 */
public class MainActivity extends AppCompatActivity {

    // Used to load the 'jni' library on application startup.
    static {
        System.loadLibrary("jni");
    }

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // Example of a call to a native method
        TextView tv = binding.sampleText;
        tv.setText(stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'jni' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    public void jumpTo(View view) {
        switch (view.getId()) {
            case R.id.btn_dyna_register:
                openActivity(MainActivity.this, DynamicRegisterActivity.class);
                break;
            case R.id.btn_nprocess_jdata:
                openActivity(MainActivity.this, ProcessJavaDataActivity.class);
                break;
            case R.id.btn_interact:
                new ProcessJavaDataHelper().runNativeCallJava(this);
                break;
            default:
                break;
        }
    }

    private void openActivity(Context context, Class clz) {
        Intent intent = new Intent(context, clz);
        startActivity(intent);
    }

    //给Native 反射调用
    public void updateUI(){
        Log.e("CrazyMoJNI","MainActivity#updateUI");
        if (Looper.myLooper() == Looper.getMainLooper()){
            Toast.makeText(this,"更新UI",Toast.LENGTH_SHORT).show();
        }else{
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,"更新UI",Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}